package ;

import haxe.Http;
import haxe.remoting.HttpAsyncConnection;
import js.Cart;
import js.Dom;
import js.Lib;
import me.cunity.data.Cookie;

import data.ShopLoader;

/**
 * ...
 * @author Axel Huizinga axel@cunity.me
 */

class Shop 
{
	static var useCookies:Bool = Cookie.isEnabled();
	//var useCookies:Bool;
	var cart:Cart;
	var proxy:ApiProxy;
	var session:Hash<String>;
	var shopLoader:ShopLoader;

	var topWin:Window;
	
	public function new() 
	{
		topWin = Lib.window;
		topWin.updateContent = updateContent;
		
		cart = new Cart(topWin);
		topWin.cart = cart;
		//Lib.alert(cart + ':' + cart.cookiesEnabled);
		if (false && !cart.cookiesEnabled)
		{
			var URL = "server.php";
			var cnx:HttpAsyncConnection = HttpAsyncConnection.urlConnect(URL);
			cnx.setErrorHandler( 
				function(err) {
					#if debug
					trace("Error : " + Std.string(err));
					#end
				}
			);
				
			//trace("EasyCookie:" + Type.typeof(untyped __js__("EasyCookie")));
			
			trace (Lib.window.location.href + ':' + topWin.location.href + ':' + (topWin.session == null));
			
			//topWin.onscroll = resetBee;
			
			proxy = new ApiProxy(cnx.Server);
			if (topWin.session == null)
			{
				topWin.xContent = Xml.parse(Http.requestUrl('site/home.xml?id=ttt' + Std.int(Math.random() * 10000)));
				proxy.initSession(new DateFormat().text, processResult);
				trace(useCookies);
				if(!useCookies)
					Lib.window.onunload = saveSession;	
				
			}
		}
		else 
		{//COOKIE BASED SESSION - JS ONLY
			topWin.xContent = Xml.parse(Http.requestUrl('site/home.xml?id=ttt' + Std.int(Math.random() * 10000)));
			try {
				//Lib.alert('load cart.xml');
				cart.init(Xml.parse(Http.requestUrl(('site/cart.xml?id=ttt' + Std.int(Math.random() * 10000)))));
				//cart.init(null);
				topWin.onunload = cart.saveSession;
			}
			catch (ex:Dynamic)
			{
				trace("Konnte Warenkorb nicht für die Anzeige vorbereiten:" + ex);
				//Lib.alert(ex);
			}
		}
	}
	
	function reloadContent()
	{
		var iFrame:Frame = null;
		if (topWin.frames.length>0 && topWin.frames[0] != null )
		{
			//iFrameDoc = topWin.frames.mainFrame.document;
			//topWin.frames.mainFrame.onscroll = resetBee;
			//
			iFrame = topWin.frames.mainFrame;
			if (iFrame != null)
				untyped iFrame.location.reload();			
			trace( untyped iFrame.location);
		}
	}
	
	function processResult(res:Dynamic)
	{
		switch(res.get('apiMethod'))
		{
			case 'initSession':
			res.remove('apiMethod');
			topWin.session = cast(res);
			trace (topWin.location.href + ':' + topWin.session);
			for (k in topWin.session.keys())
				trace(k + ':' + topWin.session.get(k));
		}
	}
	
	function updateContent(win:Window, contentBox:HtmlDom)
	{
		//Lib.alert( win +':' + contentBox);
		//contentBox.innerHTML = "Hi Kunde :-)";
		if (shopLoader == null)
			shopLoader = new ShopLoader(topWin);
		shopLoader.create( win, contentBox);
		cart.updateCart(shopLoader.shopPage);
	}
	
	/*function resetBee(evt:Dynamic)
	{
		var bee:HtmlDom = topWin.frames.mainFrame.contentDocument.getElementById('decoBox');
		trace(bee.id + ':' + bee.offsetTop + ' + ' + bee.offsetHeight);
		topWin.document.getElementById('haxe:trace').style.display = 'block';
		untyped topWin.keepAtBottom(bee);
	}*/
	
	function saveSession(?evt:Dynamic)
	{
		proxy.saveSession();
	}
	
}